﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using RTM.API;

namespace Hoguet.RTM.WCF
{
    public class RtmMetricRestService : IRtmMetricRestService
    {
        private ITaskMetricsService _metricsService;
        public RtmMetricRestService()
        {
            //TOOD: get this stuff into IOC and app settings
            string apiKey = ConfigurationManager.AppSettings["apiKey"];
            string sharedSecret = ConfigurationManager.AppSettings["sharedSecret"];
            string name = ConfigurationManager.AppSettings["name"];
            string token = ConfigurationManager.AppSettings["token"];
            double defaultEstimateInHours = Convert.ToDouble(ConfigurationManager.AppSettings["defaultEstimateInHours"]);
            bool filterOutCompleted = Convert.ToBoolean(ConfigurationManager.AppSettings["filterOutCompleted"]);
            _metricsService = new TaskMetricsService(new RTMServiceFacade(new RTMService(new ApplicationInfo(apiKey, sharedSecret, name, token)), new RtmTaskFacadeConverter(new RtmTimeEstimator(defaultEstimateInHours)), filterOutCompleted));
        }

        public IEnumerable<Metric<DateTime, double>> GetWeeksHoursRemaining()
        {
            return _metricsService.GetSevenDaysHoursRemaining().Select(metric => new Metric<DateTime, double>(){Context = metric.Context, Value = metric.Value});
        }

        public IEnumerable<Metric<string, int>> GetHabits()
        {
            return _metricsService.GetHabits();
        }

        public IEnumerable<Metric<string, int>> GetOldest()
        {
            return _metricsService.GetOldest();
        }

        public IEnumerable<Metric<string, DateTime?>> GetTagged()
        {
            return _metricsService.GetTagged();
        }
    }
}